/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.spreadsheet.eval;

import cn.ac.nercis.pes.community.component.spreadsheet.PesSheetCellType;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.controlsfx.control.spreadsheet.GridBase;
import org.controlsfx.control.spreadsheet.SpreadsheetCell;
import org.controlsfx.control.spreadsheet.SpreadsheetCellType;
import org.controlsfx.control.spreadsheet.SpreadsheetView;

import java.util.Arrays;

/**
 * 测评结果记录表调研
 * @author zpy
 * @version 1.0.0
 */
public class TecEvalSheetView extends SpreadsheetView {
    private final static Integer DATA_COUNT = 5;

    private final ObservableList<String> dList;
    private final ObservableList<String> akList;

    public TecEvalSheetView(){
        this.getStyleClass().add("pes-sheet-view");
        dList = FXCollections.observableArrayList("符合","不符合","不适用");
        akList = FXCollections.observableArrayList("符合","不符合");

        var grid = new GridBase(DATA_COUNT, 8);
        buildGrid(grid);
        setGrid(grid);

        getColumns().get(0).setPrefWidth(170);
        getColumns().get(1).setPrefWidth(120);
        getColumns().get(2).setPrefWidth(350);
        getColumns().get(3).setMaxWidth(100);
        getColumns().get(4).setMaxWidth(100);
        getColumns().get(5).setMaxWidth(100);
        getColumns().get(6).setMaxWidth(100);
        getColumns().get(7).setMaxWidth(120);

        grid.spanColumn(4,0,3);
        grid.spanRow(2,0,0);
        grid.spanRow(2,0,1);
        grid.spanRow(2,0,2);
        grid.spanRow(2,0,7);

        grid.spanRow(2,2,0);
        grid.spanRow(2,2,7);
    }

    protected void buildGrid(GridBase grid){
        ObservableList<ObservableList<SpreadsheetCell>> rows = FXCollections.observableArrayList();
        generateColumnData(rows);
        generateTestData1(rows);
        generateTestData2(rows);
        generateTestData3(rows);
        grid.setRows(rows);
    }

    private void generateColumnData(ObservableList<ObservableList<SpreadsheetCell>> rows){
        ObservableList<SpreadsheetCell> title1 = FXCollections.observableArrayList();
        title1.addAll(generateTitleCell("测评指标",0,0),
                generateTitleCell("测评对象",0,1),
                generateTitleCell("结果记录",0,2),
                generateTitleCell("量化指标",0,3),
                generateTitleCell("量化指标",0,4),
                generateTitleCell("量化指标",0,5),
                generateTitleCell("量化指标",0,6),
                generateTitleCell("测评单元得分",0,7));
        rows.add(title1);
        ObservableList<SpreadsheetCell> title2 = FXCollections.observableArrayList();
        title2.addAll(generateTitleCell("测评指标",1,0),
                generateTitleCell("测评对象",1,1),
                generateTitleCell("结果记录",1,2),
                generateTitleCell("密码使用安全\nD",1,3),
                generateTitleCell("密码算法/技术合规性\nA",1,4),
                generateTitleCell("密钥管理安全\nK",1,5),
                generateTitleCell("测评对象评分",1,6),
                generateTitleCell("测评单元得分",1,7));
        rows.add(title2);
    }

    private void generateTestData1(ObservableList<ObservableList<SpreadsheetCell>> rows){
        ObservableList<SpreadsheetCell> data = FXCollections.observableArrayList();
        data.addAll(generateTitleCell("身份鉴别",2,0),
                generateDataCell("沈阳特种设备检测研究院机房-电子门禁系统",2,1,false),
                generateTextAreaDataCell("经访谈和实地查看，沈阳特检院OA系统部署在沈阳特种设备检测研究院机房，采用门禁ID卡，但未采用密码技术实现进出人员的身份鉴别。 ",2,2),
                generateComboBoxDataCell(2,3,dList,"符合"),
                generateComboBoxDataCell(2,4,akList,"不符合"),
                generateComboBoxDataCell(2,5,akList,"不符合"),
                generateDataCell("0",2,6,false),
                generateDataCell("0",2,7,false));
        rows.add(data);
    }

    private void generateTestData2(ObservableList<ObservableList<SpreadsheetCell>> rows){
        ObservableList<SpreadsheetCell> data = FXCollections.observableArrayList();
        data.addAll(generateTitleCell("身份鉴别",3,0),
                generateDataCell("沈阳特种设备检测研究院机房-指纹识别系统",3,1,false),
                generateTextAreaDataCell("记录功能描述。 ",3,2),
                generateComboBoxDataCell(3,3,dList,"符合"),
                generateComboBoxDataCell(3,4,akList,"不符合"),
                generateComboBoxDataCell(3,5,akList,"不符合"),
                generateDataCell("0",3,6,false),
                generateDataCell("0",3,7,false));
        rows.add(data);
    }

    private void generateTestData3(ObservableList<ObservableList<SpreadsheetCell>> rows){
        ObservableList<SpreadsheetCell> data = FXCollections.observableArrayList();
        data.addAll(generateTitleCell("电子门禁记录数据存储完整性",4,0),
                generateDataCell("沈阳特种设备检测研究院机房-电子门禁系统",4,1,false),
                generateTextAreaDataCell("经访谈负责人，沈阳特检院OA系统部署在沈阳特种设备检测研究院机房，未采用密码技术实现电子门禁系统记录数据的完整性保护。",4,2),
                generateComboBoxDataCell(4,3,dList,"符合"),
                generateComboBoxDataCell(4,4,akList,"不符合"),
                generateComboBoxDataCell(4,5,akList,"不符合"),
                generateDataCell("0",4,6,false),
                generateDataCell("0",4,7,false));
        rows.add(data);
    }

    private SpreadsheetCell generateTitleCell(String text,Integer rowIndex,Integer columnIndex){
        var cell = SpreadsheetCellType.STRING
                .createCell(rowIndex,columnIndex,1,1,text);
        cell.setEditable(false);
        cell.getStyleClass().add("header-column");
        cell.setWrapText(true);
        return cell;
    }

    private SpreadsheetCell generateDataCell(String text,Integer rowIndex,Integer columnIndex,Boolean edit){
        return generateDataCell(text,rowIndex,columnIndex,"data-cell",edit);
    }

    private SpreadsheetCell generateDataCell(String text,Integer rowIndex,Integer columnIndex,String css,Boolean edit){
        var cell = SpreadsheetCellType.STRING
                .createCell(rowIndex,columnIndex,1,1,text);
        cell.getStyleClass().add(css);
        cell.setEditable(edit);
        cell.setWrapText(true);
        return cell;
    }

    private SpreadsheetCell generateTextAreaDataCell(String text,Integer rowIndex,Integer columnIndex){
        var cell = PesSheetCellType.TEXT_AREA
                .createCell(rowIndex,columnIndex,1,1,text);
        cell.getStyleClass().addAll(Arrays.asList("data-cell-left","max-data-cell"));
        cell.setWrapText(true);
        return cell;
    }


    private SpreadsheetCell generateComboBoxDataCell(Integer rowIndex,Integer columnIndex,ObservableList<String> items,String value){
        var cell = PesSheetCellType.LIST(items)
                .createCell(rowIndex,columnIndex,1,1,value);
        cell.getStyleClass().add("data-cell");
        return cell;
    }
}